/*     */ package com.zimbra.cs.index;
/*     */ 
/*     */ 
/*     */ 
/*     */ public class Fragment
/*     */ {
/*     */   private static final int MAX_FRAGMENT_LENGTH = 150;
/*     */   
/*     */ 
/*     */   private static final String CALENDAR_SEPARATOR = "*~*~*~*~*~*~*~*~*~*";
/*     */   
/*     */ 
/*     */   private static final String STOPWORD_REPLY_1 = "--Original Message--";
/*     */   
/*     */   private static final String STOPWORD_REPLY_2 = "-- Original Message --";
/*     */   
/*     */   private static final String STOPWORD_REPLY_3 = "________________________________";
/*     */   
/*     */   private static final String STOPWORD_REPLY_4 = "--Original Invite--";
/*     */   
/*     */   private static final String STOPWORD_FORWARD_1 = "--Forwarded Message--";
/*     */   
/*     */   private static final String STOPWORD_FORWARD_2 = "-- Forwarded Message --";
/*     */   
/*     */ 
/*     */   public static enum Source
/*     */   {
/*  28 */     MESSAGE,  APPOINTMENT,  NOTEBOOK;
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     private Source() {}
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   private static boolean isHeader(String text, boolean allowSpaces)
/*     */   {
/*  42 */     for (int i = 0; i < text.length(); i++) {
/*  43 */       char c = text.charAt(i);
/*  44 */       if (c == ':')
/*  45 */         return i != 0;
/*  46 */       if ((c == '\r') || (c == '\n'))
/*  47 */         return false;
/*  48 */       if ((!allowSpaces) && (Character.isWhitespace(c)))
/*  49 */         return false;
/*     */     }
/*  51 */     return false;
/*     */   }
/*     */   
/*     */   private static String skipFragmentHeader(String content, boolean calendar) {
/*  55 */     String fragment = content;String checkpoint = content;
/*     */     
/*     */ 
/*  58 */     int returnIndex = -1;
/*  59 */     while (fragment.length() > 0)
/*     */     {
/*  61 */       char first = fragment.charAt(0);
/*  62 */       fragment = fragment.trim();
/*  63 */       if ((first == '\r') || (first == '\n')) {
/*  64 */         checkpoint = fragment;
/*     */       }
/*  66 */       returnIndex = fragment.indexOf('\n');
/*  67 */       if (!isHeader(fragment, calendar)) {
/*  68 */         if (!calendar)
/*     */           break;
/*  70 */         String line = returnIndex == -1 ? fragment : fragment.substring(0, returnIndex).trim();
/*  71 */         if (line.equals("*~*~*~*~*~*~*~*~*~*"))
/*  72 */           checkpoint = returnIndex == -1 ? "" : fragment.substring(returnIndex + 1).trim();
/*  73 */         break;
/*     */       }
/*     */       
/*  76 */       if (returnIndex == -1) {
/*  77 */         checkpoint = "";
/*  78 */         break;
/*     */       }
/*  80 */       fragment = fragment.substring(returnIndex + 1);
/*     */     }
/*     */     
/*  83 */     return checkpoint.length() > 0 ? checkpoint : content;
/*     */   }
/*     */   
/*     */   private static String skipQuotedText(String content, boolean twoLineHeader) {
/*  87 */     String fragment = content;
/*     */     
/*     */ 
/*  90 */     int returnIndex = -1;
/*  91 */     int headerLines = twoLineHeader ? 2 : 1;
/*  92 */     for (int i = 0; i < headerLines; i++) {
/*  93 */       if ((fragment.startsWith(">")) || (fragment.startsWith("|")))
/*     */         break;
/*  95 */       returnIndex = fragment.indexOf('\n', returnIndex + 1);
/*  96 */       if (returnIndex == -1)
/*     */         break;
/*  98 */       String firstLine = fragment.substring(0, returnIndex).trim();
/*  99 */       if (firstLine.endsWith(":")) {
/* 100 */         fragment = fragment.substring(returnIndex + 1).trim();
/*     */         
/* 102 */         if (!fragment.startsWith("[")) break;
/* 103 */         returnIndex = fragment.indexOf('\n');
/* 104 */         if ((returnIndex == -1) || (!fragment.substring(0, returnIndex).trim().endsWith("]"))) break;
/* 105 */         fragment = fragment.substring(returnIndex + 1).trim(); break;
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 112 */     if (fragment.startsWith(">")) {
/*     */       do {
/* 114 */         returnIndex = fragment.indexOf('\n');
/* 115 */         fragment = returnIndex == -1 ? "" : fragment.substring(returnIndex + 1).trim();
/* 116 */       } while (fragment.startsWith(">"));
/* 117 */     } else if (fragment.startsWith("|")) {
/*     */       do {
/* 119 */         returnIndex = fragment.indexOf('\n');
/* 120 */         fragment = returnIndex == -1 ? "" : fragment.substring(returnIndex + 1).trim();
/* 121 */       } while (fragment.startsWith("|"));
/* 122 */     } else if (fragment != content) {
/* 123 */       fragment = content;
/*     */     }
/*     */     
/* 126 */     return fragment;
/*     */   }
/*     */   
/*     */   private static String compressLine(String line)
/*     */   {
/* 131 */     StringBuilder sb = new StringBuilder();
/* 132 */     char last = ' ';char lastButOne = '\000';
/* 133 */     int i = 0; for (int len = line.length(); (i < len) && (sb.length() <= 155); i++) {
/* 134 */       char c = line.charAt(i);
/*     */       
/* 136 */       if (Character.isWhitespace(c)) {
/* 137 */         c = ' ';
/*     */       }
/* 139 */       if ((c != ' ') || (last != c))
/*     */       {
/*     */ 
/* 142 */         if ((c >= ' ') && (c != 65534) && (c != 65535))
/*     */         {
/*     */ 
/* 145 */           if (c != 65532)
/*     */           {
/*     */ 
/* 148 */             if ((c < 56320) || (c > 57343))
/*     */             {
/*     */ 
/* 151 */               if ((c >= 55296) && (c <= 56319)) {
/* 152 */                 if (i != len - 1)
/*     */                 {
/* 154 */                   char c2 = line.charAt(i + 1);
/* 155 */                   if ((c2 >= 56320) && (c2 <= 57343))
/*     */                   {
/* 157 */                     sb.append(c);
/* 158 */                     c = c2;
/* 159 */                     i++;
/*     */                   }
/*     */                 }
/* 162 */               } else if (((c != '=') && (c != '-')) || (last != c) || (lastButOne != c))
/*     */               {
/*     */ 
/* 165 */                 lastButOne = last;
/* 166 */                 sb.append(last = c);
/*     */               } } } } } }
/* 168 */     return sb.toString().trim();
/*     */   }
/*     */   
/*     */   public static String getFragment(String content, boolean hasCalendar) {
/* 172 */     Source item = Source.MESSAGE;
/* 173 */     if (hasCalendar)
/* 174 */       item = Source.APPOINTMENT;
/* 175 */     return getFragment(content, item);
/*     */   }
/*     */   
/*     */   public static String getFragment(String content, Source item) {
/* 179 */     String remainder = content.trim();
/* 180 */     String backup = remainder;
/* 181 */     StringBuilder fragment = new StringBuilder();
/*     */     
/*     */     String result;
/* 184 */     if (item == Source.NOTEBOOK) {
/* 185 */       result = content;
/*     */     }
/*     */     else {
/* 188 */       if (item == Source.APPOINTMENT) {
/* 189 */         remainder = skipFragmentHeader(remainder, true);
/*     */       }
/*     */       
/* 192 */       remainder = skipQuotedText(remainder, true);
/* 193 */       boolean elided = false;boolean mainMessage = true;
/* 194 */       while (fragment.length() <= 150)
/*     */       {
/* 196 */         if ((remainder.startsWith(">")) || (remainder.startsWith("|"))) {
/* 197 */           if (mainMessage) {
/* 198 */             remainder = skipQuotedText(remainder, false);
/* 199 */             elided = true;
/*     */           } else {
/* 201 */             remainder = remainder.substring(1).trim();
/*     */           }
/*     */         }
/* 204 */         if (remainder.equals("")) {
/*     */           break;
/*     */         }
/* 207 */         int returnIndex = remainder.indexOf('\n');
/* 208 */         if (((returnIndex == 3) && (remainder.startsWith("-- "))) || ((returnIndex == 4) && (remainder.startsWith("-- \r")))) {
/*     */           break;
/*     */         }
/* 211 */         String line = (returnIndex == -1 ? remainder : remainder.substring(0, returnIndex)).trim();
/* 212 */         if ((mainMessage) && (line.endsWith(":")) && (returnIndex != -1)) {
/* 213 */           String trimmed = skipQuotedText(remainder, false);
/* 214 */           if (trimmed != remainder) {
/* 215 */             remainder = trimmed;
/* 216 */             elided = true;
/* 217 */             continue;
/*     */           }
/*     */         }
/*     */         
/* 221 */         line = compressLine(line);
/*     */         
/* 223 */         if ((line.equalsIgnoreCase("--Original Message--")) || (line.equalsIgnoreCase("-- Original Message --")) || (line.equalsIgnoreCase("________________________________")) || (line.equalsIgnoreCase("--Original Invite--")) || (line.equalsIgnoreCase("--Forwarded Message--")) || (line.equalsIgnoreCase("-- Forwarded Message --")))
/*     */         {
/*     */ 
/* 226 */           if ((fragment.length() != 0) || (returnIndex == -1)) {
/*     */             break;
/*     */           }
/* 229 */           fragment.append(line);
/* 230 */           remainder = skipFragmentHeader(remainder.substring(returnIndex).trim(), false);
/* 231 */           elided = mainMessage = 0;
/*     */         }
/*     */         else
/*     */         {
/* 235 */           if (fragment.length() != 0) {
/* 236 */             if (elided)
/* 237 */               fragment.append(" ...");
/* 238 */             fragment.append(' ');
/*     */           }
/* 240 */           fragment.append(line);
/* 241 */           remainder = returnIndex == -1 ? "" : remainder.substring(returnIndex).trim();
/* 242 */           elided = false;
/*     */         } }
/* 244 */       result = fragment.toString();
/*     */     }
/*     */     
/*     */ 
/* 248 */     if (result.equals(""))
/* 249 */       result = compressLine(backup);
/* 250 */     boolean isTruncated = result.length() > 150;
/* 251 */     String result = result.substring(0, isTruncated ? 150 : result.length());
/*     */     
/*     */ 
/*     */ 
/* 255 */     if (isTruncated) {
/* 256 */       int wspIndex = Math.max(result.lastIndexOf('\n'), result.lastIndexOf(' '));
/* 257 */       wspIndex = Math.max(wspIndex, result.lastIndexOf('\t'));
/* 258 */       if (wspIndex != -1)
/* 259 */         result = result.substring(0, wspIndex).trim();
/* 260 */       if (!result.endsWith(" ...")) {
/* 261 */         result = result.concat(" ...");
/*     */       }
/*     */     }
/* 264 */     return result;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/index/Fragment.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */